// course/pages/courseOnline/courseOnline.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icons: [{
        img: "/images/@2x_zixunpeixun.png",
        text: "咨询师培训",
        url: ''
      },
      {
        img: "/images/@2x_life.png",
        text: "智慧生活",
        url: '/course/pages/SmartLiving/SmartLiving'
      },
      {
        img: "/images/@2x_teachergroud.png",
        text: "教师板块",
        url: ''
      },
      {
        img: "/images/@2x_docter.png",
        text: "身心医生",
        url: ''
      },
      {
        img: "/images/@2x_freeclass.png",
        text: "免费公开课",
        url: ''
      },
    ],
    courseDetailData:[],
    chanceIndex: 1,
    updown: 1,
    downMenu: false,
    intervalMenu: false,
    interval:1
  },
  changeSort(e) {
    let index = e.target.dataset.index
    console.log(index)
    this.setData({
      chanceIndex: index
    })

    if(index == 1){
      this.getCourseDetailData()
    } 
    //根据人气高到底查询
    else if(index == 2){
      console.log("这是2");
      this.selectByStudyCount()
    }
    else if (index ==3) {
      this.selectByEvaluation()
    }
   else if (index == 4) {

      this.setData({
        intervalMenu: false,
        downMenu: !this.data.downMenu
      })
    } else if (index == 5) {
      this.setData({
        downMenu: false,
        intervalMenu: !this.data.intervalMenu
      })
    } else {
      this.setData({
        downMenu: false,
        intervalMenu: false
      })
    }
  },
  changePrice(e) {
    let updown = e.target.dataset.updown
    console.log(updown);
    if (updown == 1) {
      this.selectByPrice("asc")
    }else{
      this.selectByPrice("desc")
    }
    this.setData({
      updown: updown,
      downMenu: false
    })
  },
  changeInterval(e){
    let interval = e.target.dataset.interval
    this.setData({
      interval: interval,
      // intervalMenu: false
    })
    console.log(this.data.interval);
  },
  sureInterval(){
    const interval=this.data.interval
    console.log(this.data.interval);
    if (interval==1) {
      console.log("这是1");
      this.selectByInterval(0,0)
    }else if(interval ==2 ){
      console.log("这是2");
      this.selectByInterval(0,50)
    }else if(interval ==3 ){
      console.log("这是3");
      this.selectByInterval(50,100)
    }else if(interval ==4 ){
      console.log("这是4");
      this.selectByInterval(100,500)
    }else if(interval ==5 ){
      console.log("这是5");
      this.selectByInterval(500,1000)
    }else if(interval ==6 ){
      console.log("这是6");
     
      //1000以上的请求
      wx.request({
        url: 'http://localhost:8080/course/condition',
        method:"GET",
        data:{
          maxPrice:1000,
          sortType:"price",
          sortOrder:"desc"
        },
        success:(resp)=>{
          console.log(resp.data.data);
          if(resp.data.code == 200){
            this.setData({
              courseDetailData:resp.data.data
            })
          }else{
            console.log("状态码不等于200");
          }
        },
        fail:(err)=>{
          console.log("请求发送失败");
        }
      })


    }
    this.setData({
      intervalMenu: false
    })
  },
  jumpUrl(e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },


    //根据人气查询
    selectByStudyCount(){
      wx.request({
        url: 'http://localhost:8080/course/condition',
        method:"GET",
        data:{
          sortType:"studyCount",
          sortOrder:"desc"
        },
        success:(resp)=>{
          console.log(resp.data.data);
          if(resp.data.code == 200){
            this.setData({
              courseDetailData:resp.data.data
            })
          }else{
            console.log("状态码不等于200");
          }
        },
        fail:(err)=>{
          console.log("请求发送失败");
        }
      })
    },

    //根据评价查询
    selectByEvaluation(){
      wx.request({
        url: 'http://localhost:8080/course/condition',
        method:"GET",
        data:{
          sortType:"evaluationCount",
          sortOrder:"desc"
        },
        success:(resp)=>{
          console.log(resp.data.data);
          if(resp.data.code == 200){
            this.setData({
              courseDetailData:resp.data.data
            })
          }else{
            console.log("状态码不等于200");
          }
        },
        fail:(err)=>{
          console.log("请求发送失败");
        }
      })
    },
    //根据价格查询
    selectByPrice(sortOrder){
      wx.request({
        url: 'http://localhost:8080/course/condition',
        method:"GET",
        data:{
          sortType:"price",
          sortOrder:sortOrder
        },
        success:(resp)=>{
          console.log(resp.data.data);
          if(resp.data.code == 200){
            this.setData({
              courseDetailData:resp.data.data
            })
          }else{
            console.log("状态码不等于200");
          }
        },
        fail:(err)=>{
          console.log("请求发送失败");
        }
      })
    },

    //根据区间查询
    selectByInterval(minPrice,Maxprice){
      console.log("数据类型"+ typeof null);
      wx.request({

        url: 'http://localhost:8080/course/condition',
        method:"GET",
        data:{
          minPrice:minPrice,
          maxPrice:Maxprice,
          sortType:"price",
          sortOrder:"desc"
        },
        success:(resp)=>{
          console.log(resp.data.data);
          if(resp.data.code == 200){
            this.setData({
              courseDetailData:resp.data.data
            })
          }else{
            console.log("状态码不等于200");
          }
        },
        fail:(err)=>{
          console.log("请求发送失败");
        }
      })
    },


  //查询分类
  queryClassify(){
    wx.request({
      url: 'http://localhost:8080/courseSort/select',
      method:"GET",
      success:(resp)=>{
        if(resp.data.code==200){
          console.log(resp.data.data);
          //查询成功
         this.setData({
            icons:resp.data.data
         })
        }
      }
    })
  },

  //查询课程
  getCourseDetailData(){
    wx.request({
      url: 'http://localhost:8080/course/page',
      method:"GET",
      data:{
        pageNum:1,
        pageSize:5
      },
      success:(resp)=>{
        console.log(resp.data.data.list);
        if(resp.data.code==200){
          this.setData({
            courseDetailData:resp.data.data.list
          })
        }
      }
    })
  },

  navigator(e){
    console.log(e.currentTarget.dataset.id);
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/course/pages/courseDetail/courseDetail?id=${id}`,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.queryClassify()
      this.getCourseDetailData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})